package com.app.studentActivity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.R;
import com.app.bean.TushuBean;
import com.app.util.AdapterUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Tushu extends AppCompatActivity {

    private List<TushuBean> list;
    private RecyclerView recy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tushu);
        initView();
        initData();
        setAdapter();
    }

    private void setAdapter() {
        AdapterUtil<TushuBean> adapter = new AdapterUtil<>(R.layout.tushu_item, ((data, position, holder) -> {
            TextView name = holder.getView(R.id.name);
            TextView state = holder.getView(R.id.state);
            ImageView image = holder.getView(R.id.image);
//            List<Integer> list = new ArrayList<>();
//            list.add(R.drawable.s1);
//            list.add(R.drawable.s2);
//            list.add(R.drawable.s3);
//            list.add(R.drawable.s4);
//            Random random = new Random();
//            int a = random.nextInt(4);
            image.setImageResource(data.getPic());
            name.setText(data.getName());
            state.setText(data.getState() ? "已归还" : "未归还");
        }));
        adapter.listUpdate(list);
        recy.setAdapter(adapter);
    }

    private void initData() {
        list = new ArrayList<>();
        list.add(new TushuBean("九阳真经", true,R.drawable.s1));
//        list.add(new TushuBean("九阴白骨爪", true));
        list.add(new TushuBean("九字真言", false,R.drawable.s6));
        list.add(new TushuBean("八仙过海", true,R.drawable.s5));
        list.add(new TushuBean("水浒传", true,R.drawable.s2));
        list.add(new TushuBean("红楼梦", true,R.drawable.s4));
//        list.add(new TushuBean("三国演义", true));
        list.add(new TushuBean("西游记", false,R.drawable.s3));
//        list.add(new TushuBean("Android从零基础到实战", false));
    }

    private void initView() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.getChildAt(0).setOnClickListener(v -> finish());
        ((TextView) toolbar.getChildAt(1)).setText("图书状态");
        recy = findViewById(R.id.recy);
        recy.setLayoutManager(new LinearLayoutManager(this));
    }
}